package Server;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import GameModel.mGame;
import GameModel.mGameStat;
import Player.mPlayer;

@Root
/**
 * 
 * @author Christopher Vogel
 *
 */
public class mRegisteredPlayers {

	private static mRegisteredPlayers singleton;

	@ElementList(name = "AllPlayers")
	private ArrayList<mPlayer> AllPlayers;

	/**
	 * Retourniert das gespeicherte Players.xml als ArrayList.
	 * 
	 * @return ArrayList
	 * @author Christopher Vogel
	 */

	public synchronized static ArrayList<mPlayer> get() {
		if (singleton == null) {
			// Laden der XML-Datei
			load();
		}
		return singleton.AllPlayers;
	}

	private mRegisteredPlayers(
			@ElementList(name = "AllPlayers") ArrayList<mPlayer> AllPlayers) {
		this.AllPlayers = AllPlayers;
	}
	/**
	 * Addiert die Punkte eines beendeten Spiels in die Statistik.
	 * 
	 * @param mGame game
	 * @author Christopher Vogel
	 */
	public synchronized static void calculateStatistics(mGame game) {
		for (mGameStat gs: game.getPlayerIngameKonto()) {
			for (mPlayer savedPlayer: get()) {
				if (savedPlayer.equals(gs.getPlayer())) {
					savedPlayer.addPoints(gs.getGamePoints());
					savedPlayer.incrmentPlayedGames();
					break;
				}
			}
		}
		save();
	}

	/**
	 * Serialisiert die ArrayList "AllPlayers" in Players.xml.
	 * 
	 * @author Christopher Vogel
	 */

	public synchronized static void save() {
		try {
			Collections.sort(singleton.AllPlayers);
			Serializer serializer = new Persister();
			File source = new File("Players.xml");
			serializer.write(singleton, source);
		} catch (Exception e) {
			System.out.println("save-error: " + e.getMessage());
		}
	}
	/**
	 * Deserialisiert alle Spieler von der Datei Players.xml.
	 * 
	 * @author Christopher Vogel
	 *
	 */


	private static void load() {
		try {
			Serializer serializer = new Persister();
			File source = new File("Players.xml");

			singleton = serializer.read(mRegisteredPlayers.class, source);
		} catch (Exception e) {
			System.out.println("load-error: " + e.getMessage());
			reset(true);
		}
	}

	/**
	 * Stellt die Standardbenutzerliste her.
	 * 
	 * @param boolean safety
	 * @author Christopher Vogel
	 */
	public static void reset(boolean safety) {
		if (safety == true) {
			mPlayer Player1 = new mPlayer("Chris Koller", "1234");
			mPlayer Player2 = new mPlayer("Chris Vogel", "1234");
			mPlayer Player3 = new mPlayer("Thomas Felder", "1234");
			mPlayer Player4 = new mPlayer("David Zimmerli", "1234");
			ArrayList<mPlayer> List = new ArrayList<mPlayer>();
			List.add(Player1);
			List.add(Player2);
			List.add(Player3);
			List.add(Player4);
			singleton = new mRegisteredPlayers(List);
			save();
		}
	}
	
//	public static void sort() {
//		Collections.sort(get());
//	}
}
